﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WishClaim.Web.Helpers;
using WishClaim.Web.Models;

namespace WishClaim.Web.Controllers
{
    public class EventController : Controller
    {
        //
        // GET: /Event/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create(EventViewModel eventViewModel)
        {
            if (!ModelState.IsValid)
            {

            }

            return View("Create", new EventViewModel(){PasswordKey = Generator.RandomString(10,false),DateOfEvent = DateTime.Now});
        }

        public ActionResult Save(EventViewModel eventViewModel)
        {

            var repository = new EventRepository();
            repository.Save(new Event()
                                {
                                    Name = eventViewModel.Name,
                                    DateOfEvent = eventViewModel.DateOfEvent,
                                    Description = eventViewModel.Description,
                                    Key = eventViewModel.PasswordKey,
                                    WishLists = new List<WishList>(),
                                    Members = new List<Member>(){new Member(){Email = eventViewModel.CurrentEmail}}
                                });

            HttpContext.Session["CurrentUser"] = eventViewModel.CurrentEmail;
            return RedirectToAction("ShowWishLists", new {Id = eventViewModel.PasswordKey});

        }

        public ActionResult ShowWishLists(string id)
        {
            var repository = new EventRepository();
            var apa = repository.GetById(id);
            return View("ShowWishLists", new EventViewModel(){WishLists = apa.WishLists});
        }
        


        private EventViewModel CreateEventViewModel()
        {
            return new EventViewModel(){PasswordKey = Generator.RandomString(10,false),DateOfEvent = DateTime.Now};
        }
    }

    public class EventViewModel
    {
        public string Name { get; set; }

        public string CurrentEmail { get; set; }

        public DateTime DateOfEvent { get; set; }

        public string Description { get; set; }

        public string PasswordKey { get; set; }

        public List<WishList> WishLists { get; set; }
    }
}
